<template>
  <!-- 导入表 -->
  <t-dialog
    title="导入表"
    v-model:visible="visible"
    width="800px"
    top="5vh"
    attach="body"
  >
    <t-form :data="queryParams" ref="queryRef" layout="inline" class="mb20">
      <t-form-item label="表名称" prop="tableName">
        <t-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="表描述" prop="tableComment">
        <t-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>
    <t-row>
      <t-table
        @row-click="clickRow"
        ref="table"
        :data="dbTableList"
        row-key="tableName"
        :columns="columns"
        @select-change="handleSelectionChange"
        height="260px"
      >
      </t-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </t-row>
    <template #footer>
      <div class="dialog-footer">
        <t-button theme="primary" @click="handleImportTable">确 定</t-button>
        <t-button theme="default" @click="visible = false">取 消</t-button>
      </div>
    </template>
  </t-dialog>
</template>

<script setup>
import { listDbTable, importTable } from "@/api/tool/gen";

const total = ref(0);
const visible = ref(false);
const tables = ref([]);
const dbTableList = ref([]);
const { proxy } = getCurrentInstance();

const columns = ref([
  {
    colKey: "row-select",
    type: "multiple",
    title: "选择",
    width: 55,
    align: "center",
  },
  { colKey: "tableName", title: "表名称", ellipsis: true, align: "center" },
  { colKey: "tableComment", title: "表描述", ellipsis: true, align: "center" },
  { colKey: "createTime", title: "创建时间", width: 160, align: "center" },
  { colKey: "updateTime", title: "更新时间", width: 160, align: "center" },
]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  tableName: undefined,
  tableComment: undefined,
});

const emit = defineEmits(["ok"]);

/** 查询参数列表 */
function show() {
  getList();
  visible.value = true;
}
/** 单击选择行 */
function clickRow(row) {
  proxy.$refs.table.toggleRowSelection(row);
}
/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  tables.value = selectedRowKeys;
}
/** 查询表数据 */
function getList() {
  listDbTable(queryParams).then((res) => {
    dbTableList.value = res.rows;
    total.value = res.total;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 导入按钮操作 */
function handleImportTable() {
  const tableNames = tables.value.join(",");
  if (tableNames == "") {
    proxy.$modal.msgError("请选择要导入的表");
    return;
  }
  importTable({ tables: tableNames }).then((res) => {
    proxy.$modal.msgSuccess(res.msg);
    if (res.code === 200) {
      visible.value = false;
      emit("ok");
    }
  });
}

defineExpose({
  show,
});
</script>
